<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header class="header">
      <div>
        <img src="../assets/heima.png" alt="" />
        <span style="font-size: 16px; color: #fff; margin-left: 10px"
          >电商后台管理系统</span
        >
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边 -->
      <el-aside :width="flag ? '64px' : '200px'" class="aside">
        <!-- 折叠切换 -->
        <div class="toggle_aside" @click="toggleFn">|||</div>
        <el-menu
          router
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse-transition="false"
          :collapse="flag"
        >
          <el-submenu :index="item.id + ''" v-for="item in list" :key="item.id">
            <!-- 一级 -->
            <template slot="title">
              <i :class="iconObj[item.id]" style="margin-right: 10px"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveActive('/' + subitem.path)"
            >
              <i class="el-icon-menu" style="margin-right: 10px"></i>
              <span>{{ subitem.authName }}</span></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 中间 -->
      <el-main class="main">
        <!-- 二级路由占位 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      activePath: '',
      flag: false,
      list: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
    }
  },
  methods: {
    // 保存当前激活样式
    saveActive(path) {
      sessionStorage.setItem('thisPath', path)
      this.activePath = path
    },
    loginout() {
      // 退出清空本地会话存储
      sessionStorage.clear()
      // 强制跳转
      this.$router.push('/login')
    },
    async getList() {
      // console.log('获取侧边栏')
      const { data: res } = await this.$ajax.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$Msg.error(res.meta.msg)
      this.list = res.data
    },
    toggleFn() {
      console.log(123)
      this.flag = !this.flag
    },
  },
  created() {
    this.getList()
    this.activePath = sessionStorage.getItem('thisPath')
  },
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.header {
  background-color: #0863be;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  div {
    display: flex;
    align-items: center;
  }
}
.aside {
  background-color: #313743;
}
.main {
  background-color: #fff;
}
.el-menu {
  border-right: none;
}

.toggle_aside {
  font-size: 16px;
  color: #fff;
  line-height: 24px;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2em;
  background-color: green;
}
</style>
